<template>
  <div class="tab-box rules">
    <div class="rules-title">
      <span class="title">
        <!-- {{ $srv.getI18nByPath('piece.oConditionsSetting.condition') }} -->
        {{ $srv.getI18nByValue('统计字段配置') }}
      </span>
      <Button size="small" type="primary" @click="addConditions">
        {{ $srv.getI18nByValue('添加统计字段') }}</Button
      >
    </div>

    <div class="rules-cell">
      <!-- v-if="item.conditions" -->
      <template>
        <div
          class="rules-cons"
          v-for="(cons, idx) of config"
          :key="'con' + idx"
        >
          <div class="rules-cons-title">
            {{ $srv.getI18nByValue('字段') }}
            {{ idx + 1 }}:
          </div>
          <div class="rules-cons-inputs">
            <div class="rules-cons-fields">
              <i-input
                :placeholder="
                  $srv.getI18nByPath('piece.oChartSetting.fieldname')
                "
                v-model="cons.field"
                @on-blur="saveChanges"
              >
              </i-input>
              <Select
                clearable
                :placeholder="
                  $srv.getI18nByPath('piece.oRulesSetting.conditiontype')
                "
                v-model="cons.type"
                @on-change="feildTypeChange($event, idx)"
              >
                <Option value="number">{{
                  $srv.getI18nByPath('piece.oChartSetting.number')
                }}</Option>
                <Option value="text">{{
                  $srv.getI18nByPath('piece.oRulesSetting.text')
                }}</Option>
                <!-- <Option value="date">{{
                  $srv.getI18nByPath('piece.oChartSetting.date')
                }}</Option>
                <Option value="daterange">{{
                  $srv.getI18nByPath('piece.oRulesSetting.daterange')
                }}</Option>
                <Option value="null">{{
                  $srv.getI18nByPath('piece.oRulesSetting.null')
                }}</Option> -->
              </Select>
            </div>
            <div
              class="rules-cons-values"
              v-if="cons.type && cons.type !== '' && cons.type !== 'null'"
            >
              <div class="items">
                <div
                  class="rules-cons-values-item"
                  v-for="(item, iidx) of cons.conditions"
                  :key="'ope' + iidx"
                >
                  <Select
                    clearable
                    :placeholder="
                      $srv.getI18nByPath('piece.oRulesSetting.operator')
                    "
                    @on-change="saveChanges"
                    v-model="item.operator"
                  >
                    <Option
                      v-for="(operator, oidx) of operators[cons.type]"
                      :key="oidx"
                      :value="operator.value"
                      >{{ operator.label }}</Option
                    >
                  </Select>
                  <i-input
                    @on-blur="saveChanges"
                    :placeholder="
                      $srv.getI18nByPath('piece.oRulesSetting.conditionvalue')
                    "
                    v-model="item.value"
                  >
                  </i-input>
                  <Icon
                    type="md-close"
                    @click="removeConditionOperator(cons.conditions, iidx)"
                  />
                </div>
              </div>
              <Icon
                type="ios-add-circle"
                @click="addConditionOperator(cons.conditions)"
              />
            </div>
          </div>
          <div class="rules-cons-btns">
            <Icon type="md-close" @click="removeRulesCondition(idx)" />
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Watch, Model, Vue } from 'vue-property-decorator';
import { Tsrv } from '@/services';

@Component
export default class oRulesSetting extends Vue {
  @Model('modelchange') config: any;

  // 操作符
  private operators = {
    number: [
      {
        value: '==',
        label: Tsrv.getI18nByPath('vxe.filter.equal')
      },
      {
        value: '!=',
        label: Tsrv.getI18nByPath('vxe.filter.unequal')
      },
      {
        value: '>',
        label: Tsrv.getI18nByPath('vxe.filter.more')
      },
      {
        value: '>=',
        label: Tsrv.getI18nByPath('vxe.filter.moreorequal')
      },
      {
        value: '<',
        label: Tsrv.getI18nByPath('vxe.filter.less')
      },
      {
        value: '<=',
        label: Tsrv.getI18nByPath('vxe.filter.lessorequal')
      }
      // {
      //   value: 'between',
      //   label: Tsrv.getI18nByPath('vxe.filter.between')
      // },
      // {
      //   value: 'nbetween',
      //   label: Tsrv.getI18nByPath('vxe.filter.notbetween')
      // }
    ],
    text: [
      {
        value: 'has',
        label: Tsrv.getI18nByPath('vxe.filter.include')
      },
      {
        value: 'nhas',
        label: Tsrv.getI18nByPath('vxe.filter.notinclude')
      }
    ]
    // date: [
    //   {
    //     value: 'has',
    //     label: Tsrv.getI18nByPath('vxe.filter.include')
    //   },
    //   {
    //     value: 'nhas',
    //     label: Tsrv.getI18nByPath('vxe.filter.notinclude')
    //   }
    // ],
    // daterange: [
    //   {
    //     value: 'between',
    //     label: Tsrv.getI18nByPath('vxe.filter.between')
    //   },
    //   {
    //     value: 'nbetween',
    //     label: Tsrv.getI18nByPath('vxe.filter.notbetween')
    //   }
    // ]
  };

  mounted() {}

  private saveChanges() {
    this.$emit('modelchange', this.config);
    this.$emit('change');
  }

  private clearFeildType(idx) {
    this.config[idx].conditions = [];
  }

  private feildTypeChange(e, idx) {
    if (e === 'undefined' || e === 'null' || e === '') {
      // this.config[idx].conditions = [];
      this.config[idx].conditions.length = 0;
    }
    if (e && this.config[idx].conditions.length === 0) {
      this.config[idx].conditions.push({
        operator: '',
        value: ''
      });
    }
    this.saveChanges();
  }

  /**
   * 增加条件
   */
  private addConditions() {
    this.config.push({
      field: '',
      type: '',
      conditions: [
        // {
        //   operator: '',
        //   value: '',
        // },
      ]
    });
    this.saveChanges();
  }

  private addConditionOperator(conditions: any[]) {
    conditions.push({
      operator: '',
      value: ''
    });
    this.saveChanges();
  }

  /**
   * 移除规则条件
   */
  private removeRulesCondition(index: number) {
    this.config.splice(index, 1);
    this.saveChanges();
  }

  private removeConditionOperator(conditions: any[], index: number) {
    conditions.splice(index, 1);
    this.saveChanges();
  }
}
</script>

<style lang="scss" scoped>
.rules {
  .rules-title {
    width: 100%;
    height: 30px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    .title {
      float: left;
      font-weight: 400;
      height: 30px;
      line-height: 30px;
    }
    button {
      float: right;
      margin: 6px 0;
    }
  }
  .rules-cell {
    background-color: #fdfdfd;
    padding: 0 10px;
    // margin-bottom: 10px;
    .ivu-icon {
      cursor: pointer;
    }
    .rules-cons {
      margin: 10px 0 0;
      display: flex;
      .rules-cons-title {
        line-height: 30px;
        flex-shrink: 0;
      }
      .rules-cons-inputs {
        margin: 0 10px;
        flex: 1;
        .rules-cons-fields {
          margin-bottom: 10px;
          display: flex;
          > div:first-child {
            margin-right: 10px;
          }
        }
        .rules-cons-values {
          position: relative;
          .items {
            width: 100%;
            display: flex;
            flex-direction: column;
            justify-content: start;
            align-items: start;
            .rules-cons-values-item {
              width: 100%;
              display: flex;
              flex-direction: row;
              justify-content: start;
              align-items: center;
              margin-bottom: 10px;
              // > div:first-child {
              //   margin-right: 10px;
              // }
              > div {
                margin-right: 10px;
              }
              > .ivu-icon {
              }
            }
          }
          > .ivu-icon {
            position: absolute;
            right: -22px;
            top: 10px;
            cursor: pointer;
          }
        }
      }
      .rules-cons-btns {
        flex-shrink: 0;
        line-height: 30px;
      }
    }
  }
}
</style>
